<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从文件指针中读取一行并过滤掉 HTML 标记</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.fgets.html">« fgets</a></li>
      <li style="float: right;"><a href="function.file-exists.html">file_exists »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>从文件指针中读取一行并过滤掉 HTML 标记</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.fgetss" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fgetss</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">fgetss</span> &mdash; <span class="dc-title">从文件指针中读取一行并过滤掉 HTML 标记</span></p>

 </div>

 <div id="function.fgetss-refsynopsisdiv">
   <div class="warning"><strong class="warning">警告</strong><p class="simpara">本函数已自 PHP 7.3.0
起<em class="emphasis">弃用</em>，自 PHP 8.0.0 起<em class="emphasis">移除</em>。强烈建议不要依赖本函数。</p></div>
 </div>

 <div class="refsect1 description" id="refsect1-function.fgetss-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fgetss</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code><span class="initializer"> = ?</span></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$allowable_tags</code><span class="initializer"> = ?</span></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   和 <span class="function"><a href="function.fgets.html" class="function">fgets()</a></span> 相同，只除了 <span class="function"><strong>fgetss()</strong></span> 尝试从读取的文本中去掉任何 HTML 和 PHP 标记。
   The function retains the parsing state from call to call, and as such is not
   equivalent to calling <span class="function"><a href="function.strip-tags.html" class="function">strip_tags()</a></span> on the return value of
   <span class="function"><a href="function.fgets.html" class="function">fgets()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fgetss-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">handle</code></dt>

     <dd>

      <p class="para">文件指针必须是有效的，必须指向由
<span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 或 <span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span> 成功打开的文件(并还未由 <span class="function"><a href="function.fclose.html" class="function">fclose()</a></span> 关闭)。</p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       取回该长度的数据。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">allowable_tags</code></dt>

     <dd>

      <p class="para">
       可以用可选的第三个参数指定哪些标记不被去掉。
       See <span class="function"><a href="function.strip-tags.html" class="function">strip_tags()</a></span> for details regarding
       <code class="parameter">allowable_tags</code>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fgetss-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   从 <code class="parameter">handle</code> 指向的文件中大读取 <code class="parameter">length</code> - 1 个字节的字符，并过滤了所有的 HTML 和 PHP 代码。
  </p>
  <p class="para">
   错误发生时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fgetss-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2154">
    <p><strong>示例 #1 一行行读取一个 PHP 文件</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOD<br /></span><span style="color: #DD0000">&lt;html&gt;&lt;body&gt;<br />&nbsp;&lt;p&gt;Welcome!&nbsp;Today&nbsp;is&nbsp;the&nbsp;&lt;?php&nbsp;echo(date('jS'));&nbsp;?&gt;&nbsp;of&nbsp;&lt;?=&nbsp;date('F');&nbsp;?&gt;.&lt;/p&gt;<br />&lt;/body&gt;&lt;/html&gt;<br />Text&nbsp;outside&nbsp;of&nbsp;the&nbsp;HTML&nbsp;block.<br /></span><span style="color: #007700">EOD;<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'sample.php'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;@</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"sample.php"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$buffer&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgetss</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4096</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
 Welcome! Today is the  of .

Text outside of the HTML block.
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.fgetss-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">在读取在 Macintosh 电脑中或由其创建的文件时， 如果 PHP
不能正确的识别行结束符，启用运行时配置可选项 <a href="filesystem.configuration.html#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a>
也许可以解决此问题。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.fgetss-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
    <li class="member"><span class="function"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
    <li class="member"><span class="function"><a href="function.popen.html" class="function" rel="rdfs-seeAlso">popen()</a> - 打开进程文件指针</span></li>
    <li class="member"><span class="function"><a href="function.fsockopen.html" class="function" rel="rdfs-seeAlso">fsockopen()</a> - 打开 Internet 或者 Unix 套接字连接</span></li>
    <li class="member"><span class="function"><a href="function.strip-tags.html" class="function" rel="rdfs-seeAlso">strip_tags()</a> - 从字符串中去除 HTML 和 PHP 标签</span></li>
    <li class="member"><span class="methodname"><a href="splfileobject.fgetss.html" class="methodname" rel="rdfs-seeAlso">SplFileObject::fgetss()</a> - Gets line from file and strip HTML tags</span></li>
    <li class="member"><a href="filters.string.html#filters.string.strip_tags" class="link">string.strip_tags</a> 过滤</li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>